<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BABranchCooker
 *
 */
class BABranchCooker extends BATypedEventCooker {

    protected function getTypeOwnerName() {
        return 'branch';
    }

    protected function getOwnerTableName() {
        return 'branch';
    }

    public function addBranch($branchData, $branchType = NULL, $branchId = NULL) {

        $branchTable = $this->getTreeTable($branchType);
        $branch = NULL;
        if (is_numeric($branchId) && $branchId != NULL) {
            $branch = $this->getBranch($branchId);
        }
        //find branch by id, if need other way to identify branch, 
        //can add other code to identify branch follow same logic
        if (is_array($branchData) && $branchData != NULL) {
            if (isset($branchData['id'])) {
                $branchId = $branchData['id'];
                unset($branchData['id']);
            }
            if (isset($branchData['branchId'])) {
                $branchId = $branchData['branchId'];
                unset($branchData['branchId']);
            }
            if ($branch == NULL && $branchId != NULL) {
                $branch = $this->getBranch($branchId);
            }
        }
        if ($branch == NULL) {//add new list
            return $branchTable->addList($branchData);
        } else {
            if ($branch instanceof BTreeList) {
                return $branch->addItems($branchData);
            }
        }
        return FALSE;
    }

    public function getBranch($branchData, $type = NULL) {
        $branchTable = $this->getTreeTable($type);
        if (is_numeric($branchData)) {
            $branchId = $branchData;
            return $branchTable->getListById($branchId);
        }
        if (is_array($branchData)) {
            return $branchTable->getList(array('itemData' => $branchData));
        }
        if (is_string($branchData)) {
            $branchName = $branchData;
            $itemData = array();
            $itemData['name'] = $branchName;
            return $branchTable->getList(array('itemData' => $itemData));
        }
        return FALSE;
    }

    public function getCurrentBranch() {
        $branchId = $this->activeLink->getLinkVariable('branchId');

        $branch = $this->getBranch($branchId);
        if ($branch != NULL) {
            Blyn::app()->getActiveOperation()->addContextVariable('branchId', $branchId);
            return $branch;
        } else {
            $branchId = Blyn::app()->getActiveOperation()->getContextVariable('branchId');
            $branch = $this->getBranch($branchId);
            return $branch;
        }
    }

    public function getBranchList($branchType = NULL, $branchData = NULL) {
        $branchTable = $this->getTreeTable($branchType);

        if ($branchData != NULL && is_array($branchData)) {
            return $branchTable->getView(array('itemData' => $branchData));
        }

        if ($branchData == NULL) {
            return $branchTable->getView();
        }

        return array();
    }

    public function onShowAdminHome() {
        
    }

    public function onShowCustomerHome() {
        
    }

    public function onShowMemberHome() {
        
    }

}
